﻿package common.serialization;

import java.math.BigDecimal;
import util.StringUtil;

public class ExtXmlDataBean extends XmlDataBean {
	public static final String XH = "XH";
	public static final String ORDERTYPE = "OT";
	public static final String ORDERTYPE_VALUE_TREE_NUM = "TREE_NUM";
	private Object otherObject = null;

	public Object getOtherObject() {
		return otherObject;
	}

	public void setOtherObject(Object otherObject) {
		this.otherObject = otherObject;
	}

	@Override
	public void appendChildXmlDataBean(XmlDataBean xmlDataBea) {
		String OTValue = xmlDataBea.getAttribute(ORDERTYPE);
		String xh_value = xmlDataBea.getAttribute(XH);
		if (StringUtil.isNull(xh_value)) {
			super.appendChildXmlDataBean(xmlDataBea);
		} else {
			appendSortChildXmlDataBean(xmlDataBea, xh_value);
		}
		if (ORDERTYPE_VALUE_TREE_NUM.equals(OTValue)) {
			orderType_Tree_Num();
		}
	}

	private void appendSortChildXmlDataBean(XmlDataBean xmlDataBea,
			String xh_value) {
		xmlDataBea.setParentDataBean(this);
		// childXmlDataBeanList.add(xmlDataBea);
		boolean isInsert = false;
		int len = super.childXmlDataBeanList.size();
		if (len == 0) {
			childXmlDataBeanList.add(xmlDataBea);
			isInsert = true;
		}
		BigDecimal xh1 = new BigDecimal(xh_value);
		for (int i = 0; i < len; i++) {
			XmlDataBean XDB = (XmlDataBean) childXmlDataBeanList.get(i);
			BigDecimal xh2 = new BigDecimal(XDB.getAttribute(XH));

			// if(xh2.subtract(xh1).longValue()>0){
			if (xh2.compareTo(xh1) == 1) {
				childXmlDataBeanList.add(i, xmlDataBea);
				isInsert = true;
				break;
			}
		}
		if (!isInsert) {
			childXmlDataBeanList.add(xmlDataBea);
		}

	}

	private void orderType_Tree_Num() {
		String selfOTValue = this.getAttribute(ORDERTYPE);
		if ("root".equals(this.getId()) || "0".equals(this.getId())
				|| selfOTValue == null || "".equals(selfOTValue)) {
			selfOTValue = "";
			// System.out.println("====================================Row:=this.getId()=="+this.getId());
		}
		this.setAttribute(ORDERTYPE, selfOTValue);
		// System.out.println("====================================selfOTValue:==="+selfOTValue);
		// if (otherObject instanceof Row){
		// Row row = (Row)otherObject;
		// row.getData().put(ORDERTYPE, this.getAttribute(ORDERTYPE));
		// }

		int len = super.childXmlDataBeanList.size();
		for (int i = 0, j = 1; i < len; i++) {
			ExtXmlDataBean XDB = (ExtXmlDataBean) childXmlDataBeanList.get(i);
			String OTValue = XDB.getAttribute(ORDERTYPE);
			if (OTValue == null || "".equals(OTValue)) {
				continue;
			}
			Object extOtherObject = XDB.getOtherObject();
			String orderNum = "".equals(selfOTValue) ? ("" + j++) : selfOTValue
					+ "." + (j++);

			XDB.setAttribute(ORDERTYPE, orderNum);
			// if (extOtherObject instanceof Row){
			// Row row = (Row)extOtherObject;
			// row.getData().put(ORDERTYPE, orderNum);
			// }

		}

	}

}
